package com.thingcom.delinrobot;

import android.app.Application;
import android.content.Context;
import android.os.Build;

import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.tencent.bugly.crashreport.CrashReport;
import com.thingcom.delinrobot.Data.GizManger;
import com.thingcom.delinrobot.utils.Density;
import com.thingcom.delinrobot.utils.LanguageUtil;
import com.thingcom.delinrobot.utils.SpUtil;
import com.thingcom.delinrobot.utils.WifiUtils;
import com.xuhao.android.libsocket.sdk.OkSocket;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;

import me.yokeyword.fragmentation.Fragmentation;

public class DelinApplication extends Application {

    public static final String APP_ID = "9557ce7dd362420689d8334e813db40e";
    public static final String APP_S = "6408a989a1684170a8925d09b86215aa";

    public static final String PRODUCT_KEY = "3804d69257614f12a3ea2ef8d0fc0730";
    public static final String PRODUCT_S = "023332763441450496be513b03ea631e";
    private static DelinApplication INSTANCE;

    public static synchronized DelinApplication getINSTANCE(){
        return INSTANCE;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;


        ConcurrentHashMap<String, String> appInfo = new ConcurrentHashMap<>();
        appInfo.put("appId",APP_ID);
        appInfo.put("appSecret",APP_S);

        List<ConcurrentHashMap<String, String>> productInfo = new LinkedList<>();
        ConcurrentHashMap<String,String> proInfo = new ConcurrentHashMap<>();
        proInfo.put("productKey",PRODUCT_KEY);
        proInfo.put("productSecret",PRODUCT_S);

        productInfo.add(proInfo);

        /**
         * productInfo如果填写了，系统会根据设备判断过滤
         */
        GizWifiSDK.sharedInstance().startWithAppInfo(getApplicationContext(),appInfo,productInfo,null,true);
        CrashReport.initCrashReport(getApplicationContext(),"174f760fa3",false);
        WifiUtils wifiUtils = new WifiUtils(this);
        Density.setDensity(this);
        //.debug(false)
        Fragmentation fragmentation = Fragmentation.builder()
                .stackViewMode(Fragmentation.BUBBLE)
                .debug(BuildConfig.IS_SHOW_LOG)
                //.debug(false)
                .install();
        GizManger.getInstance().init(getApplicationContext());

        //socket
        OkSocket.initialize(this, BuildConfig.IS_SHOW_LOG);


        /*
         * 对于7.0以下，需要在Application创建的时候进行语言切换
         */
        String language;
        language = SpUtil.getInstance(this).getString(SpUtil.LANGUAGE);

        if (language.equals("")) {
            Locale locale = getContext().getResources().getConfiguration().locale;
            language = locale.getLanguage();

        }
        /*
         * 如果是7.0以下，我们需要调用changeAppLanguage方法，
         * 如果是7.0及以上系统，直接把我们想要切换的语言类型保存在SharedPreferences中即可
         * 然后重新启动MainActivity
         * @param language
         */
        SpUtil.getInstance(getContext()).putString(SpUtil.LANGUAGE, language);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {

            LanguageUtil.changeAppLanguage(DelinApplication.getContext(), language);
        }
    }

    public static Context getContext() {
        return INSTANCE;
    }
}
